package com.aurora.source.authority.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aurora.source.R;
import com.aurora.source.authority.adapter.ProcessAuthSelectAdapter;
import com.aurora.source.authority.adapter.SysAuthSelectAdapter;
import com.aurora.source.authority.bean.ProcessDataBean;
import com.aurora.source.authority.bean.ProcessListBean;
import com.aurora.source.authority.bean.SysAuthDataBean;
import com.aurora.source.authority.bean.SysAuthListBean;
import com.aurora.source.authority.presenter.AddJurisdictionPresenter;
import com.aurora.source.authority.view.IAddJurisdictionView;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.util.DpPxUtil;
import com.aurora.source.util.SPUtils;
import com.aurora.source.widget.flowlayout.FlowLayoutManager;
import com.aurora.source.widget.flowlayout.SpaceItemDecoration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 权限选择
 */
public class AddJurisdictionActivity extends BaseWhiteToolbarActivity<AddJurisdictionPresenter> implements IAddJurisdictionView {

    @BindView(R.id.rv_auth_data)
    RecyclerView rvAuthData;
    @BindView(R.id.rv_sys_data)
    RecyclerView rvSysData;

    @BindView(R.id.button)
    AppCompatButton button;
    ProcessAuthSelectAdapter processAuthSelectAdapter; // 流程权限选择
    SysAuthSelectAdapter sysAuthSelectAdapter;//系统权限选择
    List<ProcessListBean> selectProcess;//已选择的流程权限
    List<SysAuthListBean> selectSysAuths;//已选择的系统权限

    @Override
    public AddJurisdictionPresenter attach() {
        return new AddJurisdictionPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_add_jurisdiction;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        selectProcess = (List<ProcessListBean>) getIntent().getSerializableExtra("selectProcess");
        selectSysAuths = (List<SysAuthListBean>) getIntent().getSerializableExtra("selectSysAuths");
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        simpleToolbar.setMainTitle("权限管理-添加授权");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        processAuthSelectAdapter =  new ProcessAuthSelectAdapter();
        sysAuthSelectAdapter = new SysAuthSelectAdapter();

        rvAuthData.setLayoutManager(new FlowLayoutManager());
        rvAuthData.setAdapter(processAuthSelectAdapter);
        rvAuthData.addItemDecoration(new SpaceItemDecoration(DpPxUtil.dp2px(this, 5)));

        rvSysData.setLayoutManager(new FlowLayoutManager());
        rvSysData.setAdapter(sysAuthSelectAdapter);
        rvSysData.addItemDecoration(new SpaceItemDecoration(DpPxUtil.dp2px(this, 5)));


    }

    @Override
    public void doBusiness() {
        Map<String, String> params = new HashMap<>();
        params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
        basePresenter.getFlowPermission(activity, params);
        basePresenter.getSysPermission(activity, params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onGetFlowPermissionSuccess(ProcessDataBean data) {
        if(selectProcess !=null&&data!=null&&data.getProcessList()!=null){
            for(ProcessListBean e: selectProcess){
                for(ProcessDataBean.ProcessListBean m:data.getProcessList()){
                    if(m.getProcessId().equals(e.getProcessId())){
                        m.setSelect(true);
                    }
                }
            }
        }
        if(data!=null){
            processAuthSelectAdapter.setDataList(data.getProcessList());
        }
    }

    @Override
    public void onGetFlowPermissionFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onGetSysPermissionFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onGetSysPermissionSuccess(SysAuthDataBean data) {
        if(selectSysAuths !=null&&data!=null&&data.getSystemRoleDTOList()!=null){
            for(SysAuthListBean e: selectSysAuths){
                for(SysAuthDataBean.SystemRoleDTOListBean m:data.getSystemRoleDTOList()){
                    if(m.getSysRoleCode().equals(e.getSysRoleCode())){
                        m.setSelect(true);
                    }
                }
            }
        }
        if(data!=null){
            sysAuthSelectAdapter.setDataList(data.getSystemRoleDTOList());
        }
    }

    @OnClick({R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button:

                List<ProcessDataBean.ProcessListBean> data1 = new ArrayList<>();
                List<SysAuthDataBean.SystemRoleDTOListBean> data2 = new ArrayList<>();

                for(int i=0;i<processAuthSelectAdapter.getDataList().size();i++){
                    if(processAuthSelectAdapter.getDataList().get(i).isSelect()){
                        data1.add(processAuthSelectAdapter.getDataList().get(i));
                    }
                }

                for(int i=0;i<sysAuthSelectAdapter.getDataList().size();i++){
                    if(sysAuthSelectAdapter.getDataList().get(i).isSelect()){
                        data2.add(sysAuthSelectAdapter.getDataList().get(i));
                    }
                }

                Intent intent = new Intent();
                intent.putExtra("data1", (Serializable) data1);
                intent.putExtra("data2", (Serializable) data2);
                setResult(RESULT_OK,intent);
                hideSoftInput();
                finish();
                break;
        }
    }
}
